分类
联系方式
  1. 新浪微博
  2. E-mail

Flutter AndroidNativeWindow

介绍

源码位于:shell/platform/android/surface/android_native_window.h

Android 侧创建好的 Surface,在 C 层被封装成 AndroidNativeWindow 对象。也就是说,这个类指代一个 Java SurfaceView 中的 Surface。

私有属性

window_

类型为 Handle,实际上是 Java 的 Surface 对象。

Handle window_;

在构造方法中传入:

explicit AndroidNativeWindow(Handle window);

使用场景

AndroidSurfaceGL::SetNativeWindow

这是 C 层的 Surface,位于 shell/platform/android/android_surface_gl.cc:

bool AndroidSurfaceGL::SetNativeWindow(
    fml::RefPtr<AndroidNativeWindow> window) {
  native_window_ = window;
  // Create the onscreen surface.
  onscreen_surface_ = android_context_->CreateOnscreenSurface(window);
  if (!onscreen_surface_->IsValid()) {
    return false;
  }
  return true;
}

AndroidSurfaceGL 会保留 window 实例。同时调用 C 层 SurfaceContext 的 CreateOnscreenSurface 创建一个 onScreen Surface。

AndroidContextGL::CreateOnscreenSurface

位于 shell/platform/android/android_context_gl.cc:

std::unique_ptr<AndroidEGLSurface> AndroidContextGL::CreateOnscreenSurface(
    fml::RefPtr<AndroidNativeWindow> window) const {
  EGLDisplay display = environment_->Display();

  const EGLint attribs[] = {EGL_NONE};

  EGLSurface surface = eglCreateWindowSurface(
      display, config_, reinterpret_cast<EGLNativeWindowType>(window->handle()),
      attribs);
  return std::make_unique<AndroidEGLSurface>(surface, display, context_);
}

environment 的类型为 AndroidEnvironmentGL。 eglCreateWindowSurface 是 OpenGL ES 中的方法,参见文档《EGLSurface 和 OpenGL ES》,引用描述:

EGLSurface 可以是由 EGL 分配的离屏缓冲区(称为“pbuffer”),也可以是由操作系统分配的窗口。调用 eglCreateWindowSurface() 函数可创建 EGL 窗口 Surface。eglCreateWindowSurface() 将“窗口对象”作为参数,在 Android 上,该对象是 Surface。Surface 是 BufferQueue 的生产方端。消费方(SurfaceView、SurfaceTexture、TextureView 或 ImageReader)创建 Surface。当您调用 eglCreateWindowSurface() 时,EGL 将创建一个新的 EGLSurface 对象,并将其连接到窗口对象的 BufferQueue 的生产方接口。此后,渲染到该 EGLSurface 会导致一个缓冲区离开队列、进行渲染,然后排队等待消费方使用。

通过引用可以看出,EGLSurface 是一个可供 OpenGL 绘制的 Surface,并且跟 window(Java 侧的 Surface)建立了绑定关系。

PlatformViewAndroid::NotifyCreated

AndroidNativeWindow 实例是在 platform_view_android_jni_impl.cc 的 SurfaceCreated 方法中创建的。创建完成后便会传入这个方法中:

void PlatformViewAndroid::NotifyCreated(
    fml::RefPtr<AndroidNativeWindow> native_window) {
  if (android_surface_) {
    InstallFirstFrameCallback();

    fml::AutoResetWaitableEvent latch;
    fml::TaskRunner::RunNowOrPostTask(
        task_runners_.GetRasterTaskRunner(),
        [&latch, surface = android_surface_.get(),
         native_window = std::move(native_window)]() {
          surface->SetNativeWindow(native_window);
          latch.Signal();
        });
    latch.Wait();
  }

  PlatformView::NotifyCreated();
}

android_surface_ 的类型是 AndroidSurface,这个是 Native Surface 层的容器,也就是 AndroidSurfaceGL(当使用 OpenGL 渲染的时候)。

可以看到第一帧绘制回调也是在这个方法里初始化的。

切到 Raster GPU 线程,调用 AndroidSurfaceGL::SetNativeWindow 进行 Native Surface 与 Java Surface 的绑定。